﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 药品信息管理
{
    public partial class 药品直入直出 : Form
    {
        public 药品直入直出()
        {
            InitializeComponent();
        }

        private void 药品直入直出_Load(object sender, EventArgs e)
        {
            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.ReadOnly = true;
            this.dataGridView1.MultiSelect = false;
            fresh();  
        }

        private void button7_Click(object sender, EventArgs e)//关闭
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)//刷新
        {
            fresh();

        }

        private void button2_Click(object sender, EventArgs e)//进入
        {
            Form f = new Form();
            try
            {
                f = new 药品直入直出单录入((int)dataGridView1.CurrentRow.Cells["入库单号"].Value);
            }
            catch  { return; }
            f.ShowDialog();
            fresh();
        }

        private void button3_Click(object sender, EventArgs e)//新单
        {
            int rkdh = 0;
            try
            {

                string cmdtext1 = "select rkdh from  yk_rkd0  order by rkdh desc ";
                SqlConnection cn = new SqlConnection(SQL.conStr);
                {
                    cn.Open();
                    SqlCommand com = new SqlCommand(cmdtext1, cn);
                    rkdh = (int)com.ExecuteScalar();
                }
            }
            catch { MessageBox.Show("首次入库"); }
            Form f = new 药品直入直出单录入(rkdh + 1);
            f.ShowDialog();
            fresh();
        }

        private void button4_Click(object sender, EventArgs e)//删除
        {
            try
            {
                SqlConnection cn = new SqlConnection(SQL.conStr);
                cn.Open();
                string cmdtext3 = "   BEGIN TRANSACTION  " +
                                  "   Delete Yk_RKD0 where RKDH=" + dataGridView1.CurrentRow.Cells["入库单号"].Value +
                                  "   Delete Yk_RKD1 where RKDH=  " + dataGridView1.CurrentRow.Cells["入库单号"].Value +
                                  "   Delete Yk_CKD0 where CKDH=" + dataGridView1.CurrentRow.Cells["出库单号"].Value +
                                  "   Delete Yk_CKD1 where CKDH=  " + dataGridView1.CurrentRow.Cells["出库单号"].Value +
                                  "   COMMIT TRANSACTION ";
                SqlCommand cmd = new SqlCommand(cmdtext3, cn);
                cmd.ExecuteNonQuery();
                fresh();
            }
            catch  { return; } 
        }

        private void button8_Click(object sender, EventArgs e)//审核
        {
            try
            {
                SqlConnection cn = new SqlConnection(SQL.conStr);
                cn.Open();
                string cmdtext3 = "   BEGIN TRANSACTION  " +
                              "UPDATE YK_RKD0 SET " +
                              "SHRY = '" + LoginForm.username + "', " +
                              "JZRY = '', " +
                              "FLAG = 17  " +
                              "WHERE RKDH = " + dataGridView1.CurrentRow.Cells["入库单号"].Value +
                              "  UPDATE YK_CKD0 SET " +
                              "SHRY = '" + LoginForm.username + "', " +
                              "JZRY = '', " +
                              "FLAG = 17  " +
                              "WHERE CKDH = " + dataGridView1.CurrentRow.Cells["出库单号"].Value +
                              "   COMMIT TRANSACTION ";
                SqlCommand cmd = new SqlCommand(cmdtext3, cn);
                cmd.ExecuteNonQuery();
                fresh();
            }
            catch  { return; } 
        }

        private void button5_Click(object sender, EventArgs e)//消审
        {
            try
            {
                SqlConnection cn = new SqlConnection(SQL.conStr);
                cn.Open();
                string cmdtext3 = "   BEGIN TRANSACTION  " +
                              "UPDATE YK_RKD0 SET " +
                              "SHRY = '', " +
                              "JZRY = '', " +
                              "FLAG = 16  " +
                              "WHERE RKDH = " + dataGridView1.CurrentRow.Cells["入库单号"].Value +
                              "  UPDATE YK_CKD0 SET " +
                              "SHRY = '', " +
                              "JZRY = '', " +
                              "FLAG = 16  " +
                              "WHERE CKDH = " + dataGridView1.CurrentRow.Cells["出库单号"].Value +
                              "   COMMIT TRANSACTION "; 
              
                SqlCommand cmd = new SqlCommand(cmdtext3, cn);
                cmd.ExecuteNonQuery();
                fresh();
            }
            catch { return; } 

        }

        private void button6_Click(object sender, EventArgs e)//记账
        {
            try
            {
                int jzrq = 0;//记账日期
                SqlConnection cn1 = new SqlConnection(SQL.conStr);
                {
                    cn1.Open();
                    string cmdtext4 = "select  convert(int,(select Convert(DateTime,getdate())))+1";
                    SqlCommand cmd = new SqlCommand(cmdtext4, cn1);
                    jzrq = (int)cmd.ExecuteScalar();
                }
                SqlConnection cn2 = new SqlConnection(SQL.conStr);
                {
                    cn2.Open();
                    string cmdtext3 = "BEGIN TRANSACTION  " +
                                      "UPDATE YK_RKD0 SET " +
                                      "JZRY = '"+LoginForm.username+"', " +
                                      "FLAG = 19,  " +
                                      "jzrq =" + jzrq +
                                      "  WHERE RKDH = " + dataGridView1.CurrentRow.Cells["入库单号"].Value +
                                       "UPDATE YK_CKD0 SET " +
                                      "JZRY = '" + LoginForm.username + "', " +
                                      "FLAG = 19,  " +
                                      "jzrq =" + jzrq +
                                      "  WHERE CKDH = " + dataGridView1.CurrentRow.Cells["出库单号"].Value +
                                      "   COMMIT TRANSACTION ";

                    SqlCommand cmd = new SqlCommand(cmdtext3, cn2);
                    cmd.ExecuteNonQuery();
                }
                MessageBox.Show("记账成功");

            }
            catch { return; }
            fresh();

        }

        public void selectRow()//选中某行后，审核 记账 按钮的显示
        {
            if (dataGridView1.SelectedRows[0].Cells["状态"].Value.ToString().Trim() == "已记账")
            {
                button4.Enabled = false;
                button5.Enabled = false;
                button6.Enabled = false;
                button8.Enabled = false;
            }
            else if (dataGridView1.SelectedRows[0].Cells["状态"].Value.ToString().Trim() == "已审核")
            {
                button4.Enabled = false;
                button5.Enabled = true;
                button6.Enabled = true;
                button8.Enabled = false;
            }
            else if (dataGridView1.SelectedRows[0].Cells["状态"].Value.ToString().Trim() == "未审核")
            {
                button4.Enabled = true;
                button5.Enabled = false;
                button6.Enabled = false;
                button8.Enabled = true;
            }
        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            selectRow();
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                for (int index = 0; index < dataGridView1.RowCount; index++)
                {
                    try
                    {
                        if (textBox1.Text.ToString().Trim() == dataGridView1.Rows[index].Cells["入库单号"].Value.ToString().Trim())
                        {
                            this.dataGridView1.Rows[index].Selected = true;
                            break;
                        }
                    }
                    catch 
                    {
                        return;
                    }

                }
            }
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                for (int index = 0; index < dataGridView1.RowCount; index++)
                {
                    try
                    {
                        if (textBox2.Text.ToString().Trim() == dataGridView1.Rows[index].Cells["序号"].Value.ToString().Trim())
                        {
                            this.dataGridView1.Rows[index].Selected = true;
                            break;
                        }
                    }
                    catch 
                    {
                        return;
                    }

                }
            }
        }

        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)//双击，进入
        {
            Form f = new Form();
            try
            {
                f = f = new 药品直入直出单录入((int)dataGridView1.CurrentRow.Cells["入库单号"].Value);
            }
            catch  { return; }
            f.ShowDialog();
            fresh();

        }

        public void fresh()//刷新
        {
            DataTable dt = new DataTable();
            string dtp1 = dateTimePicker1.Value.ToShortDateString();
            string dtp2 = dateTimePicker2.Value.ToShortDateString();
            SqlConnection cn = new SqlConnection(SQL.conStr);
            string addtext1, addtext2;
            if (checkBox1.Checked)
            {
                if (checkBox2.Checked)
                {
                    if (checkBox3.Checked)
                        addtext1 = "  AND (r0.flag=16 OR r0.flag=17 OR r0.flag=19)  ";
                    else addtext1 = " AND (r0.flag=16 OR r0.flag=17)  ";
                }
                else
                {
                    if (checkBox3.Checked)
                        addtext1 = "  (AND r0.flag=16 OR r0.flag=19)  ";
                    else addtext1 = "  AND r0.flag=16  ";
                }
            }
            else
            {
                if (checkBox2.Checked)
                {
                    if (checkBox3.Checked)
                        addtext1 = "  AND (r0.flag=17 OR r0.flag=19)  ";
                    else addtext1 = "  AND r0.flag=17  ";
                }
                else
                {
                    if (checkBox3.Checked)
                        addtext1 = "  AND r0.flag=19   ";
                    else addtext1 = "  ";
                }
            }
            addtext2 = "-1";
           
            if (checkBox5.Checked)
            {
                addtext2 = addtext2 + ",2";
            }
           
            if (checkBox7.Checked)
            {
                addtext2 = addtext2 + ",1";
            }
            
            if (checkBox9.Checked)
            {
                addtext2 = addtext2 + ",6";
            }

            try
            {
                cn.Open();
                string cmdtext1 = "  select distinct r0.rkdh as 入库单号,r0.xh as 序号,(select  convert(datetime,r0.rq-2)) as 制单日期,CR.MC AS 入库类型,gys.mc as '供应商/退药科室'," +
                                "  r0.ckdh as  出库单号,ks.mc  as 领用科室,r0.lrry as 录入,r0.shry as 审核,r0.jzry as 记账,r0.bz 备注, rkflag.mc as 状态  FROM YK_RKD0 R0 LEFT JOIN YK_CRLX CR ON R0.RKLX = CR.BM LEFT JOIN YK_ZDGYS gys ON R0.gysbm = gys.BM   " +
                                "  LEFT JOIN zd_rkflag rkflag ON R0.flag = rkflag.flag  LEFT JOIN YK_CKD0 C0 ON R0.CKDH = C0.CKDH  LEFT JOIN zd_ks ks ON ks.bm=C0.ksbm  " +
                                "WHERE R0.YKBM='402'   " +
                                addtext1 + "AND  R0.RKLX IN (" + addtext2 + ")  " +
                                "AND R0.RQ >=(select  convert(int,(select Convert(DateTime,'" + dtp1 + "')))+2 )AND R0.RQ <=(select  convert(int,(select Convert(DateTime,'" + dtp2 + "')))+2 )  ORDER BY R0.RKDH";

                SqlDataAdapter da = new SqlDataAdapter(cmdtext1, cn);
                da.Fill(dt);
                dataGridView1.DataSource = dt;
                dataGridView1.Columns[4].Width = 250;
            }
            catch { return; }
            if (dataGridView1.Rows.Count > 0)
                selectRow();
        }


    }
}